<?php

namespace app\modules\backend\controllers;

use app\models\Base;
use app\models\User;
use app\models\FeedBack;
use app\tools\Re;
use Yii;
use yii\helpers\Json;

class FeedBackController extends BaseController
{
    public $type_arr = ['', '商品相关', '物流情况', '客户服务', '优惠活动', '功能异常', '产品建议', '其他'];
    /**
     * @param string $type
     * @return array
     */
    public function actionList(){
        $page = Yii::$app->request->post('page', 0);
        $type = Yii::$app->request->post('type', -1);
        $phone = Yii::$app->request->post('phone', '');
        $content = Yii::$app->request->post('content', '');
        $query = FeedBack::find()->where(['status' => 1]);
        if ($type > 0) {
            $query->andWhere(['type' => $type]);
        }
        if ($phone) {
            $query->andWhere(['like', 'phone', $phone]);
        }
        if ($content) {
            $query->andWhere(['like', 'content', $content]);
        }
        $list = $query->orderBy("created_at desc")->limit(FeedBack::PAGE_SIZE)->offset(FeedBack::PAGE_SIZE*$page)->all();
        $feed_back_data = [];
        if ($list) {
            foreach ($list as $v) {
                $feed_back_data[] = [
                    'id' => $v->id,
                    'nickname' => $v->user->nickname,
                    'phone' => $v->phone,
                    'content' => $v->content,
                    'type_name' => $this->type_arr[$v->type],
                    'created_time' => date("Y-m-d H:i", $v->created_at),
                ];
            }
        }
        return Re::data([
            'data' => $feed_back_data,
            'totalPage' => $query->count(),
            'currentPage' => $page,
            'perPage' => FeedBack::PAGE_SIZE
        ]);
    }
}
